package user.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import java.awt.Font;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import user.dao.UserDao;
import user.model.Book;
import user.model.User;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class BookHistoryFrame extends JInternalFrame {
	private JTable table;
	private UserDao userDao = new UserDao();
	private static String loginName = null;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookHistoryFrame frame = new BookHistoryFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BookHistoryFrame() {
		setClosable(true);
		setIconifiable(true);
		setTitle("借阅历史");
		setBounds(100, 100, 734, 618);
		
		JScrollPane scrollPane = new JScrollPane();
		
		JButton btnNewButton = new JButton("返回");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 690, Short.MAX_VALUE)
						.addComponent(btnNewButton, Alignment.TRAILING))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(46)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 453, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
					.addComponent(btnNewButton)
					.addContainerGap())
		);
		
		table = new JTable();
		table.setFont(new Font("宋体", Font.PLAIN, 18));
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u56FE\u4E66\u7F16\u53F7", "\u4E66\u540D", "\u4F5C\u8005", "\u501F\u4E66\u65E5\u671F", "\u8FD8\u4E66\u65E5\u671F"
			}
		));
		scrollPane.setViewportView(table);
		getContentPane().setLayout(groupLayout);
		fillTable();
	}
	
	public void fillTable() {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
	    dtm.setRowCount(0);//设置成0行
	    //查询出书籍
	    loginName = LoginFrame.userName();
		User u = userDao.selectUname(loginName);
	    List<Book> bookList = userDao.listBookHistory(u.getId());
	    for(Book book:bookList) {
	    	Vector v = new Vector<>();
	    	v.add(dtm.getRowCount()+1);
	    	v.add(book.getBookName());
	    	v.add(book.getAuthor());
	    	v.add(book.getBtime().toString());
	    	v.add(book.getRtime().toString());
	    	
	    	dtm.addRow(v);
	    }
	}
}
